package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.itboye.textileexpes.beans.NewMeesgeBeans;
import com.itboye.textileexpes.presenter.MainCategoryTypeTwoPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.EnumUtility;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.widget.XListView;
import com.itboye.textileexpres.widget.XListView.IXListViewListener;

public class FragmentNews extends BaseFragment implements Observer,
		IXListViewListener {
	XListView list;
	AdapterCustomerService adapterCustomerService;
	MainCategoryTypeTwoPresenter presenter;
	List<NewMeesgeBeans> beans;
	int page_index = 1;
	int page_size = 10;
	LinearLayout linelyout;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_news;
	}

	@Override
	public void onMyActivityCreated() {
		onOpenBase();
	}

	public void onOpenBase() {
		// 此实现不一定要放在Application onCreate中
		final String userid = IsMobileNumPhone.isALiBABA();
		// final String userid = "tbimuser_8f85517967795eeef66c225f7883bdcb";

		// 玩儿玩儿
		// 此对象获取到后，保存为全局对象，供APP使用
		// 此对象跟用户相关，如果切换了用户，需要重新获取
		final YWIMKit mIMKit = YWAPI.getIMKitInstance(userid,
				IsMobileNumPhone.BauChuanKey);
		// 开始登录
		String password = IsMobileNumPhone.isPasswrd();
		IYWLoginService loginService = mIMKit.getLoginService();
		YWLoginParam loginParam = YWLoginParam.createLoginParam(
				IsMobileNumPhone.isUid(), password);
		loginService.login(loginParam, new IWxCallback() {

			@Override
			public void onSuccess(Object... arg0) {
				@SuppressWarnings("deprecation")
				Intent intent = mIMKit.getConversationActivityIntent();// 获取会话列表
				startActivity(intent);
				// userid是客服帐号，第一个参数是客服帐号，第二个是组ID，如果没有，传0
				// EServiceContact contact = new EServiceContact(userid,
				// postion);
				// //
				// 如果需要发给指定的客服帐号，不需要Server进行分流(默认Server会分流)，请调用EServiceContact对象
				// // 的setNeedByPass方法，参数为false。
				// // contact.setNeedByPass(false);
				// Intent intent = mIMKit.getChattingActivityIntent(contact);
				// startActivity(intent);
			}

			@Override
			public void onProgress(int arg0) {
				// TODO Auto-generated method stub
				System.out.println(arg0 + ">>yw");

			}

			@Override
			public void onError(int errCode, String description) {
				System.out.println(">>>>>>>>>>>>>>>>" + errCode + "\n"
						+ description);
			}
		});

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == MainCategoryTypeTwoPresenter.Messge_success) {
				beans = (List<NewMeesgeBeans>) entity.getData();
				if (beans == null || beans.size() <= 0) {
					linelyout.setVisibility(View.VISIBLE);
					list.setVisibility(View.GONE);
				} else {
					linelyout.setVisibility(View.GONE);
					list.setVisibility(View.VISIBLE);
					adapterCustomerService = new AdapterCustomerService(
							getActivity(), beans);
					list.setAdapter(adapterCustomerService);
				}

			}
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page_size = 10;
		presenter.onMesssge(IsMobileNumPhone.isUid(),
				EnumUtility.IsMessge.getValue() + "", page_index + "",
				page_size + "");
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page_size++;
		presenter.onMesssge(IsMobileNumPhone.isUid(),
				EnumUtility.IsMessge.getValue() + "", page_index + "",
				page_size + "");
	}

}
